<template>
    <div>

        <p>上传地址</p>
        <el-input v-model="imgUpload" placeholder="请输入上传地址" />
        <p>type值</p>
        <el-input v-model="type.type" placeholder="请输入type" />

        <el-upload class="avatar-uploader"
                   :action="imgUpload"
                   :show-file-list="false"
                   :on-success="headPortraitHandleSuccess"
                   :before-upload="PictureUpload"
                   :data="type">
            <img v-if="imgActivity" :src="imgActivity" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>


        <hr style="margin-top: 100px">
        <p>图片地址</p>
        <el-input v-model="imgActivity" placeholder="请输入图片地址" />

        <img :src="imgActivity" class="avatar">
    </div>
</template>


<script>

    export default {
        data() {
            return {
                //  上传图片
                imgActivity: "",  //  图片查看地址
                imgUpload: "",    //  图片上传地址
                type: {
                    userId: 1001
                },

            };
        },
        methods: {
            //  通用 -> 上传头像时的校验
            PictureUpload(file) {  //  校验支付图片类型
                const isJPG = /image\/\w+/.test(file.type);
                if (!isJPG) return this.$message.error('请上传为图片类型的文件!');
                const isLt2M = file.size / 1024 / 1024 < 50;
                if (!isLt2M) return this.$message.error('请上传图片大小不能超过 50MB!');
                return isJPG && isLt2M;
            },
            // 上传活动图片成功回调 -> 把上传活动图片成功后的回调 md5码保存到数据库
            headPortraitHandleSuccess(response) {
                this.dialogVisibleFrom.twoCode = response.data;
                this.dialogVisibleFrom.head_portrait = response.data;
            },

            // 上传活动图片成功回调 -> 把上传活动图片成功后的回调 md5码保存到数据库
            newPortraitHandleSuccess(response) {
                this.newDialogVisibleFrom.twoCode = response.data;
                this.newDialogVisibleFrom.head_portrait = response.data;
            },
        },
        created() {
        }
    }

</script>

<style>


    /*  ------------------ 图片 ------------------ */
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        line-height: 80px;
        text-align: center;
    }

    .avatar {
        width: 80px;
        height: 80px;
        display: block;
    }


</style>
